
import 'package:find_friend/const/const.dart';
import 'package:find_friend/pages/im/pages/item/message_content_list.dart';
import 'package:flutter/material.dart';
import 'package:rongcloud_im_plugin/rongcloud_im_plugin.dart';
import 'package:rongcloud_im_plugin/src/info/message.dart';

class MsgHistoryPage extends StatefulWidget {

  final String mobile;

  const MsgHistoryPage({Key key, this.mobile}) : super(key: key);

  @override
  _MsgHistoryPageState createState() => _MsgHistoryPageState();
}

class _MsgHistoryPageState extends State<MsgHistoryPage> implements MessageContentListDelegate {

  MessageContentList messageContentList;
  List messageDataSource = new List(); //消息数组

  @override
  void initState() {
    super.initState();
    messageContentList = MessageContentList(messageDataSource, false, [], this);
    onGetHistoryMessages();
  }

  onGetHistoryMessages() async {
    List msgs = await RongcloudImPlugin.getHistoryMessage(RCConversationType.Private, widget.mobile, 0, 20);
    print("get history message");
    for(Message m in msgs) {
      messageDataSource.add(m);
    }
    _refreshMessageContentListUI();
  }

  void _refreshMessageContentListUI() {
    messageContentList.updateData(messageDataSource, false, []);
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: APP_scaffold_back_color,
      appBar: AppBar(
        title: Text("聊天记录"),
        backgroundColor: Colors.white,
        centerTitle: true,
        leading: IconButton(
          onPressed: (){
            Navigator.of(context).pop();
          },
          icon: Image.asset("assets/fanhui.png"),
        ),
        actions: [
          Image.asset("assets/sousuo.png", width: 30,),
        ],
      ),
      body: Container(
          child: messageContentList
      )
    );
  }

  @override
  void didLongPressMessageItem(Message message, Offset tapPos) {
      // TODO: implement didLongPressMessageItem
    }
  
    @override
    void didSendMessageRequest(Message message) {
      // TODO: implement didSendMessageRequest
    }
  
    @override
    void didTapItem(Message message) {
      // TODO: implement didTapItem
    }
  
    @override
    void didTapMessageItem(Message message) {
      // TODO: implement didTapMessageItem
    }
  
    @override
    void didTapMessageReadInfo(Message message) {
      // TODO: implement didTapMessageReadInfo
    }

  @override
  void willpullMoreHistoryMessage() {
    // TODO: implement willpullMoreHistoryMessage
  }

  @override
  void didTapUserPortrait(String userId) {
    // TODO: implement didTapUserPortrait
  }

  @override
  void didLongPressUserPortrait(String userId, Offset tapPos) {
    // TODO: implement didLongPressUserPortrait
  }
}